package com.ship.dispatch.util;

import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ship.common.core.utils.StringUtils;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.IshShipBaseExtend;
import liquibase.pro.packaged.C;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>UploadPortFileBusiness</b>
 *
 * @description: UploadPortFileBusiness <br>
 * @date: 2024/4/28 16:06 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */
@Component
public class UploadFreightUtil {
    private static final Logger logger = LoggerFactory.getLogger(UploadFreightUtil.class);

    public Map<String,List<IshShipBaseExtend>> uploadList(MultipartFile file) throws Exception {
        List<IshShipBaseExtend> newShipList = new ArrayList<>();
        List<IshShipBaseExtend> operationShipList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();

            ExcelReader reader = ExcelUtil.getReader(file.getInputStream(),0);
            List<Map<String, Object>> newShipMap = reader.readAll();
            newShipMap.forEach(column -> {
                IshShipBaseExtend ishShipBaseExtend = new IshShipBaseExtend();
                if (!String.valueOf(column.get("Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Status")))) {
                    ishShipBaseExtend.setStatus(String.valueOf(column.get("Status")));
                }
                if (!String.valueOf(column.get("Hull No.")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Hull No.")))) {
                    String data = String.valueOf(column.get("Hull No."));
                    if (StringUtils.isNotEmpty(data) && "N/A".equals(data)){
                        ishShipBaseExtend.setHullNo(null);
                    }else {
                        ishShipBaseExtend.setHullNo(String.valueOf(column.get("Hull No.")));
                    }
                }
                if (!String.valueOf(column.get("Type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Type")))) {
                    ishShipBaseExtend.setType(String.valueOf(column.get("Type")));
                }
                if (!String.valueOf(column.get("Dwt")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Dwt")))) {
                    ishShipBaseExtend.setDwt(Integer.parseInt(String.valueOf(column.get("Dwt"))));
                }
                if (!String.valueOf(column.get("GT")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("GT")))) {
                    ishShipBaseExtend.setGtValue(Integer.parseInt(String.valueOf(column.get("GT"))));
                }
                if (!String.valueOf(column.get("Size")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Size")))) {
                    ishShipBaseExtend.setSize(Integer.parseInt(String.valueOf(column.get("Size"))));
                }
                if (!String.valueOf(column.get("Unit")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Unit")))) {
                    ishShipBaseExtend.setUnit(String.valueOf(column.get("Unit")));
                }
                if (!String.valueOf(column.get("CGT")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("CGT")))) {
                    ishShipBaseExtend.setCgt(Integer.parseInt(String.valueOf(column.get("CGT"))));
                }
                if (!String.valueOf(column.get("Built")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Built")))) {
                    ishShipBaseExtend.setBuilt(String.valueOf(column.get("Built")));
                }
                if (!String.valueOf(column.get("Builder")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Builder")))) {
                    ishShipBaseExtend.setBuilder(String.valueOf(column.get("Builder")));
                }
                if (!String.valueOf(column.get("Yard Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Yard Status")))) {
                    String data = String.valueOf(column.get("Yard Status"));
                    if (StringUtils.isNotEmpty(data)){
                        ishShipBaseExtend.setYardStatus(HtmlUtils.htmlUnescape(data));
                    }else {
                        ishShipBaseExtend.setYardStatus(null);
                    }

                }
                if (!String.valueOf(column.get("Contract Date")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Contract Date")))) {
                    ishShipBaseExtend.setContractDate(String.valueOf(column.get("Contract Date")));
                }
                if (!String.valueOf(column.get("Company")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Company")))) {
                    ishShipBaseExtend.setCompany(String.valueOf(column.get("Company")));
                }
                if (!String.valueOf(column.get("Group Company")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Group Company")))) {
                    ishShipBaseExtend.setGroupCompany(String.valueOf(column.get("Group Company")));
                }
                if (!String.valueOf(column.get("Operator")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Operator")))) {
                    ishShipBaseExtend.setOperator(String.valueOf(column.get("Operator")));
                }
                if (!String.valueOf(column.get("NB Price")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("NB Price")))) {
                    ishShipBaseExtend.setNbPrice(new BigDecimal(String.valueOf(column.get("NB Price"))));
                }
                if (!String.valueOf(column.get("Currency")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Currency")))) {
                    String data = String.valueOf(column.get("Currency"));
                    if (StringUtils.isNotEmpty(data)){
                        ishShipBaseExtend.setCurrency(HtmlUtils.htmlUnescape(data));
                    }else {
                        ishShipBaseExtend.setCurrency(null);
                    }
                }
                if (!String.valueOf(column.get("Engine Type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Engine Type")))) {
                    ishShipBaseExtend.setEngineType(String.valueOf(column.get("Engine Type")));
                }
                if (!String.valueOf(column.get("Alternative Fuel Types")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Alternative Fuel Types")))) {
                    ishShipBaseExtend.setAlternativeFuelTypes(String.valueOf(column.get("Alternative Fuel Types")));
                }
                if (!String.valueOf(column.get("SOx Scrubber Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("SOx Scrubber Status")))) {
                    ishShipBaseExtend.setSoxScrubberStatus(String.valueOf(column.get("SOx Scrubber Status")));
                }
                if (!String.valueOf(column.get("Eco - Electronic Engine")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Eco - Electronic Engine")))) {
                    ishShipBaseExtend.setEcoElectronicEngine(String.valueOf(column.get("Eco - Electronic Engine")));
                }
                if (!String.valueOf(column.get("船舶名称")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("船舶名称")))) {
                    ishShipBaseExtend.setShipName(String.valueOf(column.get("船舶名称")));
                }
                if (!String.valueOf(column.get("IMO")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("IMO")))) {
                    ishShipBaseExtend.setImo(String.valueOf(column.get("IMO")));
                }
                ishShipBaseExtend.setDataType("1");
                ishShipBaseExtend.setEnable("1");
                ishShipBaseExtend.setCreateTime(new Date());
                ishShipBaseExtend.setUpdateTime(new Date());
                ishShipBaseExtend.setCreateUser(SecurityUtils.getUsername());
                ishShipBaseExtend.setUpdateUser(SecurityUtils.getUsername());
                newShipList.add(ishShipBaseExtend);
            });
            ExcelReader reader1 = ExcelUtil.getReader(file.getInputStream(),1);
            List<Map<String, Object>> operationShipMap = reader1.readAll();
            operationShipMap.forEach(column -> {
                IshShipBaseExtend ishShipBaseExtend = new IshShipBaseExtend();
                if (!String.valueOf(column.get("Type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Type")))) {
                    ishShipBaseExtend.setType(String.valueOf(column.get("Type")));
                }
                if (!String.valueOf(column.get("Name")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Name")))) {
                    ishShipBaseExtend.setShipName(String.valueOf(column.get("Name")));
                }
                if (!String.valueOf(column.get("Size")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Size")))) {
                    ishShipBaseExtend.setSize(Integer.parseInt(String.valueOf(column.get("Size"))));
                }
                if (!String.valueOf(column.get("Unit")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Unit")))) {
                    ishShipBaseExtend.setUnit(String.valueOf(column.get("Unit")));
                }
                if (!String.valueOf(column.get("Dwt")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Dwt")))) {
                    ishShipBaseExtend.setDwt(Integer.parseInt(String.valueOf(column.get("Dwt"))));
                }
                if (!String.valueOf(column.get("GT")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("GT")))) {
                    ishShipBaseExtend.setGtValue(Integer.parseInt(String.valueOf(column.get("GT"))));
                }
                if (!String.valueOf(column.get("Flag")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Flag")))) {
                    ishShipBaseExtend.setFlag(String.valueOf(column.get("Flag")));
                }
                if (!String.valueOf(column.get("Built")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Built")))) {
                    ishShipBaseExtend.setBuilt(String.valueOf(column.get("Built")));
                }
                if (!String.valueOf(column.get("Month")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Month")))) {
                    ishShipBaseExtend.setMonth(String.valueOf(column.get("Month")));
                }
                if (!String.valueOf(column.get("Builder")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Builder")))) {
                    ishShipBaseExtend.setBuilder(String.valueOf(column.get("Builder")));
                }
                if (!String.valueOf(column.get("Company")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Company")))) {
                    ishShipBaseExtend.setCompany(String.valueOf(column.get("Company")));
                }
                if (!String.valueOf(column.get("Group Company")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Group Company")))) {
                    ishShipBaseExtend.setGroupCompany(String.valueOf(column.get("Group Company")));
                }
                if (!String.valueOf(column.get("Operator")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Operator")))) {
                    ishShipBaseExtend.setOperator(String.valueOf(column.get("Operator")));
                }
                if (!String.valueOf(column.get("Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Status")))) {
                    ishShipBaseExtend.setStatus(String.valueOf(column.get("Status")));
                }
                if (!String.valueOf(column.get("Engine Type")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Engine Type")))) {
                    ishShipBaseExtend.setEngineType(String.valueOf(column.get("Engine Type")));
                }
                if (!String.valueOf(column.get("Alternative Fuel Types")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Alternative Fuel Types")))) {
                    ishShipBaseExtend.setAlternativeFuelTypes(String.valueOf(column.get("Alternative Fuel Types")));
                }
                if (!String.valueOf(column.get("SOx Scrubber Status")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("SOx Scrubber Status")))) {
                    ishShipBaseExtend.setSoxScrubberStatus(String.valueOf(column.get("SOx Scrubber Status")));
                }
                if (!String.valueOf(column.get("Eco - Electronic Engine")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Eco - Electronic Engine")))) {
                    ishShipBaseExtend.setEcoElectronicEngine(String.valueOf(column.get("Eco - Electronic Engine")));
                }
                if (!String.valueOf(column.get("Current Zone")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("Current Zone")))) {
                    ishShipBaseExtend.setCurrentZone(String.valueOf(column.get("Current Zone")));
                }
                if (!String.valueOf(column.get("IMO")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("IMO")))) {
                    ishShipBaseExtend.setImo(String.valueOf(column.get("IMO")));
                }
                ishShipBaseExtend.setDataType("2");
                ishShipBaseExtend.setEnable("1");
                ishShipBaseExtend.setCreateTime(new Date());
                ishShipBaseExtend.setUpdateTime(new Date());
                ishShipBaseExtend.setCreateUser(SecurityUtils.getUsername());
                ishShipBaseExtend.setUpdateUser(SecurityUtils.getUsername());

                operationShipList.add(ishShipBaseExtend);
            });
        } catch (IOException e) {

            logger.error("上传数据异常,文件解析:{}", e.getMessage());
        } catch (Exception e) {

            logger.error("上传数据异常,程序: {}", e.getMessage());
        }
        logger.info("在建船返回结果:{}"+JSONUtil.parse(newShipList));
        logger.info("在运营返回结果:{}"+JSONUtil.parse(operationShipList));
        Map<String,List<IshShipBaseExtend>> map = new HashMap<>();
        map.put("newShipList",newShipList);
        map.put("operationShipList",operationShipList);
        return map;
    }

}
